<?php

require_once '../application/controllers/Super_Controller.php';

class TagsController extends Super_Controller {

    public function indexAction() {
        $url_parts = @explode('/', $_SERVER['REDIRECT_URL']);
        array_shift($url_parts);
        $this->view->cat = !empty($url_parts[0]) ? ($url_parts[0]) : '';

        $CTM = new Application_Model_CatsAndTags();
        $cat = $CTM->getTagsByUrl($this->view->cat);
        if (!empty($cat['id'])) {
            $PGM = new Application_Model_Pages();
            $this->view->commentCnt = $PGM->getCntCommentPage();
            $onPage = 10;
            $page = (int) $this->_request->page;
            if (empty($page))
                $page = 1;

            $PGM = new Application_Model_Pages();
            //$res = $PGM->select_pn($table_name, $fields, $where, $order_by, $limit, $left_join, $group_by);
            $result = $PGM->select_pn('pages p', 'p.`id`,`category_id`,`title`, `url`, `top_menu`, `preview`, `in_main`, `view`',
                            "`active` = 1 AND `preview` != '' ",
                            '`rating` DESC',
                            ($page - 1) * $onPage,
                            $onPage,
                            "right join attach on (`obj_type` = 'page' and `obj_id` =  p.id and `target_id` = ".$cat['id']." and target_type = 'label')");

            $this->view->info = $result['result'];
            $PGM->addTagsToPage($this->view->info);
            
            $this->view->countRow = $result['total'];
            $paginator = Zend_Paginator::factory((int) $this->view->countRow);
            $paginator->setDefaultItemCountPerPage($onPage);
            $paginator->setCurrentPageNumber($page);

            $this->view->paginator = $paginator;
            $this->view->onPage = $onPage;

            $this->view->p_title = 'Developer blog :: '.$cat['title'];
            $this->view->p_key = $cat['metakeywords'];
            $this->view->p_desc = $cat['description'];
        }
    }

}
